#include "cd_inner_type.h"
#include "cd_object.h"
#include "cd_obj_pool.h"
#include "cd_entity.h"
#include "cd_type.h"
#include "cd_stack.h"
#include <cassert>

CD_NAMESPACE_BEGIN
	class	c_inner_type_null:public c_entity{
	public:
		virtual	s32			execute	(c_stack*	stack){
			c_object*	ret	=	c_obj_pool::allocate();
			stack->set_tpy_reg(ret);
			return	en_sucess;
		}

		virtual	c_entity*	clone	(c_stack* stack)
		{
			return	this;
		}

		virtual	const s8*	type_name(	){
			return	"null";
		}
	};

	class	c_inner_type_type:public c_entity{
	public:
	public:
		virtual	s32			execute	(c_stack*	stack){
			stack->throw_exception(EXCP_INVALID_PROCESS);
			return	en_sucess;
		}

		virtual	c_entity*	clone	(c_stack* stack)
		{
			return	this;
		}

		virtual	const s8*	type_name(	){
			return	"type_def";
		}
	};


	class	c_inner_type_func:public c_entity{
	public:
	public:
		virtual	s32			execute	(c_stack*	stack){
			return	en_sucess;
		}

		virtual	c_entity*	clone	(c_stack* stack)
		{
			return	this;
		}

		virtual	const s8*	type_name(	){
			return	"function";
		}
	};

	static	c_object	g_unknow_type;
	static	c_object	g_null_type(new c_inner_type_null());
	static	c_object	g_type_type(new c_inner_type_type());
	static	c_object	g_func_type(new c_inner_type_func());

	c_object*	inner_type(s32	type)
	{
		switch(type){
		case	INNER_TYPE_UNKNOW:
			return	&g_unknow_type;
		case	INNER_TYPE_NULL:
			return	&g_null_type;
		case	INNER_TYPE_TYPE:
			return	&g_type_type;
		case	INNER_FUN_TYPE:
			return	&g_func_type;
		}
		return	0;
	}
	s32			is_inner_type(c_object*	type,	s32	intype)
	{
		switch(intype){
		case	INNER_TYPE_UNKNOW:
			return	&g_unknow_type	==	type;
		case	INNER_TYPE_NULL:
			return	&g_null_type	==	type;
		case	INNER_TYPE_TYPE:
			return	&g_type_type	==	type;
		case	INNER_FUN_TYPE:
			return	&g_func_type	==	type;
		}
		return	0;
	}

CD_NAMESPACE_END